package com.orhon.smartcampus.modules.teacher.mutation;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.orhon.smartcampus.auth.JwtUtils;
import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.base.dao.SchoolsDao;
import com.orhon.smartcampus.modules.teacher.dao.TInformationDao;
import com.orhon.smartcampus.modules.teacher.dao.UndertakeProjectDao;
import com.orhon.smartcampus.modules.teacher.entity.UndertakeProject;
import com.orhon.smartcampus.modules.teacher.gqlInputModel.GQLUndertakeProjectInput;
import com.orhon.smartcampus.modules.teacher.inputMapper.UndertakeProjectInputMapper;
import com.orhon.smartcampus.modules.user.dao.TokenService;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;

@Component
public class AddUndertakeProjectMutation implements DataFetcher {

    @Autowired
    UndertakeProjectDao undertakeProjectDao;

    @Autowired
    TInformationDao tInformationDao;

    @Autowired
    SchoolsDao schoolsDao;

    @Autowired
    TokenService tokenService;


    /**
     * 添加承担课题项目
     * editor : ths
     */
    @Override
    public Object get(DataFetchingEnvironment environment) throws Exception {

        //String token = (String) SecurityUtils.getSubject().getPrincipal();
        String token = tokenService.getSystemToken();
        Long schoolId = JwtUtils.getSchoolId(token);
        GQLUndertakeProjectInput input = UndertakeProjectInputMapper.MAPPER.mapInput(environment.getArgument("inputData"));

        UndertakeProject undertakeProject = null;

        if (input.getId() != null) {
            undertakeProject = undertakeProjectDao.get(UndertakeProject.class, input.getId());
            undertakeProject.setUpdatedAt(new Date());
        } else {
            undertakeProject = new UndertakeProject();
            undertakeProject.setCreatedAt(new Date());
        }
        if(schoolId != null){

            undertakeProject.setSchool_id(schoolId);
        }
        if(input.getTeacher_id() != null){

            undertakeProject.setTeacher_id(input.getTeacher_id());
        }
        if(input.getTime() != null){

            undertakeProject.setTime(input.getTime());
        }
        if(input.getEntime() != null){

            undertakeProject.setEntime(input.getEntime());
        }
        if(input.getFund() != null){

            undertakeProject.setFund(input.getFund());
        }
        if(input.getNum() != null){

            undertakeProject.setNum(input.getNum());
        }
        if(input.getImage() != null){

            undertakeProject.setImage(input.getImage());
        }
        if(input.getShenhe() != null){

            undertakeProject.setShenhe(input.getShenhe());
        }
        if(input.getName() != null){

            undertakeProject.setName(JSON.toJSONString(input.getName()));
        }
        if(input.getUnit() != null){

            undertakeProject.setUnit(JSON.toJSONString(input.getUnit()));
        }
        if(input.getSource() != null){

            undertakeProject.setSource(JSON.toJSONString(input.getSource()));
        }
        if(input.getInputstyle() != null){

            undertakeProject.setInputstyle(JSON.toJSONString(input.getInputstyle()));
        }
        if(input.getRoal() != null){

            undertakeProject.setRoal(JSON.toJSONString(input.getRoal()));
        }
        try{
            undertakeProjectDao.saveOrUpdate(undertakeProject);
        }
        catch (Exception e){
            return Result.error(500,""+e);
        }
        return Result.ok();
    }
}

